package com.taocore.top.title.action;

import java.sql.Connection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.taobao.api.ApiException;
import com.taobao.api.domain.Item;
import com.taobao.api.response.ItemGetResponse;
import com.taocore.top.title.db.DBUtils;
import com.taocore.top.title.db.Dao;
import com.taocore.top.title.domain.RealTimeKeyword;
import com.taocore.top.title.domain.WeeklyKeyword;
import com.taocore.top.title.service.TaobaoService;
import com.taocore.top.title.service.WeeklyKeywordsService;

@Results({
	  @Result(name="success", location="keywords.jsp")
})
public class FilterKeywordsAction extends ActionBase{

	static Logger _log = Logger.getLogger(FilterKeywordsAction.class.getName());
	
	private String coreWord;
	private long numIid;
	private List<WeeklyKeyword> weeklyKeywords;
	private List<RealTimeKeyword> realtimeKeywords;
	private int limit = 50;
	private int offset;
	
	public String execute()
	{
		_log.info("coreword: " + coreWord);
		long t0 = System.currentTimeMillis();
		if (coreWord != null && !coreWord.isEmpty())
		{
			Connection conn = null;
			try
			{
				conn = DBUtils.getConnection();
				weeklyKeywords = WeeklyKeywordsService.filterWeeklyKeywords(coreWord);//Dao.INSTANCE.getWeeklyKeywordsByCoreWord(coreWord, limit, offset, conn);
				realtimeKeywords = Dao.INSTANCE.getRealTimeKeywordsByCoreWord(coreWord, limit, offset, conn);
			}
			catch (Exception e)
			{
				_log.log(Level.SEVERE, "", e);
			}
			finally
			{
				DBUtils.close(conn, null, null);
			}
		}
		else
		{
			String session = getSessionId();
			String fields = "num_iid,title,pic_url,cid";
			try {
				ItemGetResponse rsp = TaobaoService.getItem(numIid, fields, session);
				if (rsp.isSuccess())
				{
					Item item = rsp.getItem();
					//weeklyKeywords = TaobaoService.getWeeklyKeywords(item, limit, offset);
					realtimeKeywords = TaobaoService.getRealTimeKeywords(item, limit, offset);
					_log.info("realTimeKeywords.size: " + realtimeKeywords.size());
				}
				else
				{
					_log.warning(TaobaoService.getError(rsp));
				}
			} catch (ApiException e) {
				_log.log(Level.SEVERE, "", e);
			}
		}
		long t1 = System.currentTimeMillis();
		_log.info("costtttttttt: " + (t1 - t0));
		return SUCCESS;
	}

	public void setNumIid(long numIid) {
		this.numIid = numIid;
	}

	public long getNumIid() {
		return numIid;
	}
	
	public String getCoreWord() {
		return coreWord;
	}

	public void setCoreWord(String coreWord) {
		this.coreWord = coreWord;
	}

	public List<WeeklyKeyword> getWeeklyKeywords() {
		return weeklyKeywords;
	}

	public List<RealTimeKeyword> getRealtimeKeywords() {
		return realtimeKeywords;
	}
	
	public void setLimit(int limit) {
		this.limit = limit;
	}
	
	public int getLimit() {
		return limit;
	}
	
	public void setOffset(int offset) {
		this.offset = offset;
	}
	
	public int getOffset() {
		return offset;
	}
}
